home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: Strcat Doesn't work for this?
- Date: 11 Jan 1996 15:22:10 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4d4632$mj0@crl.crl.com>
- References: <Pine.SOL.3.91.960111151925.25068C-100000@lore.cs.purdue.edu>
- NNTP-Posting-Host: crl.com
-
- ** Craig Cook ** <cookca@cs.purdue.edu> writes:
-
- >Here is my excerpt of code in question:
-
- >Putword(int w, char *imageChar)
- >{
- > w = (w & 0xff);
- > strcat( imageChar, (const char *)w );
- >}
-
- >Why does it core dump? It should work right?
-
- Actually, no it shouldn't. Strcat is expecting two pointers to character,
- and you are passing it a pointer and a single byte that has been cast to
- a pointer. In actual fact, your second parameter has now become a pointer
- with a value ranging from 0-255.
-
- Remember, this second parameter should be a string. That is, a sequence
- of bytes terminated by a NULL. Assuming you're on a little-endian machine
- (which you appear to be doing -- it's /far/ from guaranteed!), if you
- change the call to strcat to look like: strcat( imageChar, (const char *)&w);
- then this code should append the low-order byte of w to the imageChar string.
-
- I think most people would consider this to be poorly-written, however. It
- invokes hardware-dependent behavior as well as only writing one byte, not
- a word on the off chance it DOES work. If you have to perform this
- operation, memcpy is probably a better way to do it -- or simply do the
- appropriate mask-and-shift operations to copy to the appropriate position
- in imageChar.
-
-